package com.bbs.gui.action.admin;

import com.bbs.db.pers.Admininfo;
import com.bbs.db.pers.Boardinfo;
import com.bbs.db.pers.Moderinfo;
import com.bbs.db.pers.Userinfo;
import com.bbs.gui.form.ALoginForm;
import com.bbs.gui.form.BBoardForm;
import com.bbs.gui.iface.AdminIface;
import com.bbs.logic.iface.BbsService;
import com.bbs.logic.iface.UserService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import java.util.List;

/**
 * @time Nov 14, 2009, 7:49:38 AM
 * @author fdcube
 */
public class AAction extends ActionSupport implements AdminIface {

    private UserService userService;
    public List<Boardinfo> bList;
    private BBoardForm bboardForm;
    public int edit = 0;
    public int bid;
    private String returnUrl;

    public String getReturnUrl() {
        return returnUrl;
    }

    public void setReturnUrl(String returnUrl) {
        this.returnUrl = returnUrl;
    }

    public BBoardForm getBboardForm() {
        return bboardForm;
    }

    public void setBboardForm(BBoardForm bboardForm) {
        this.bboardForm = bboardForm;
    }

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public int getEdit() {
        return edit;
    }

    public void setEdit(int edit) {
        this.edit = edit;
    }

    public UserService getUserService() {
        return userService;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }
    private BbsService bbsService;

    public BbsService getBbsService() {
        return bbsService;
    }

    public void setBbsService(BbsService bbsService) {
        this.bbsService = bbsService;
    }
    private ALoginForm aloginForm;

    public ALoginForm getAloginForm() {
        return aloginForm;
    }

    public void setAloginForm(ALoginForm aloginForm) {
        this.aloginForm = aloginForm;
    }

    public String Login() {
        if (userService.AdLogin(new Admininfo(aloginForm.getAname(), aloginForm.getApwd()))) {
            ActionContext.getContext().getSession().put("aname", aloginForm.getAname());
            return "loginok";
        } else {
            this.addActionError("用户名或密码错误");
            return ERROR;
        }
    }

    public String Logout() {
        try {
            ActionContext.getContext().getSession().clear();
            return "logout";
        } catch (Exception e) {
            return ERROR;
        }
    }
    public List<Userinfo> uList;

    public List<Userinfo> getuList() {
        return uList;
    }

    public void setuList(List<Userinfo> uList) {
        this.uList = uList;
    }

    public String SUsers() {
        try {
            uList = userService.GetUsers();
            System.out.println("用户数：" + uList.size());
            return "suser";
        } catch (Exception e) {
            System.out.println(e);
            return ERROR;
        }
    }
    private int uid;

    public int getUid() {
        return uid;
    }

    public void setUid(int uid) {
        this.uid = uid;
    }

    public String DUser() {
        if (userService.Delete(new Userinfo(uid))) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public String BUser() {
        Userinfo utmp = userService.GetUser(new Userinfo(uid)).get(0);
        if (userService.Block(utmp)) {
            System.out.println("return Url---------" + returnUrl);
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public String AUser() {
        Userinfo utmp = userService.GetUser(new Userinfo(uid)).get(0);
        if (userService.Authorize(utmp)) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public List<Boardinfo> getbList() {
        return bList;
    }

    public void setbList(List<Boardinfo> bList) {
        this.bList = bList;
    }

    public String SPosts() {
        try {
            bList = bbsService.GetBoards();
            return "spost";
        } catch (Exception e) {
            System.out.println(e);
            return ERROR;
        }
    }

    public String SBoards() {
        try {
            bList = bbsService.GetBoards();
            return "sboard";
        } catch (Exception e) {
            System.out.println(e);
            return ERROR;
        }
    }

    public String EBoard() {
        if (edit == 0) {
            Boardinfo btmp = bbsService.GetBoard(new Boardinfo(bid)).get(0);
            bboardForm = new BBoardForm();
            bboardForm.setBname(btmp.getBname());
            bboardForm.setBabout(btmp.getBabout());
            bboardForm.setBid(bid);
            return "eboard";
        } else {
            if (bbsService.EditBoard(new Boardinfo(bboardForm.getBid(), bboardForm.getBname(), bboardForm.getBabout()))) {
                edit = 0;
                return SUCCESS;
            } else {
                return ERROR;
            }
        }
    }

    public String ABoard() {
        if (bbsService.AddBoard(new Boardinfo(bboardForm.getBname(), bboardForm.getBabout()))) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public String DBoard() {
        if (bbsService.DeleteBoard(new Boardinfo(bid))) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }

    public String MUser() {
        if (userService.Appoint(new Userinfo(uid), new Boardinfo(bid))) {
            return SUCCESS;
        } else {
            return ERROR;
        }
    }
    public int mid;

    public int getMid() {
        return mid;
    }

    public void setMid(int mid) {
        this.mid = mid;
    }

    public String DMUser() {
        if (userService.Depose(new Moderinfo(mid)))  {
            System.out.println("删除版主成功");
            return SUCCESS;
        } else {
            System.out.println("删除版主失败");
            return ERROR;
        }
    }
}
